﻿using UnityEngine;
using System.Collections.Generic;
using Pb;
using System.Collections;
/// <summary>
/// 加入房间管理脚本
/// </summary>
public class JoinRoomModel : MonoBehaviour
{
    EginUser user = EginUser.Instance;
    public List<UISprite> SpriteNum;
    public List<GameObject> NumberClick;
    List<int> TempNum = new List<int>();
    int showCount;
    public GameObject Num_delete;
    public GameObject Num_Confirm;
    public GameObject CloseBtn;
    public GameObject Num_chongshu;
    void Awake()
    {
        for (int i = 0; i < 10; i++)
        {
            UIEventListener.Get(NumberClick[i]).onClick = (ButtonClick);
        }
        UIEventListener.Get(Num_delete).onClick = (ButtonClick);
        UIEventListener.Get(Num_Confirm).onClick = (ButtonClick);
        UIEventListener.Get(CloseBtn).onClick = (ButtonClick);

        UIEventListener.Get(Num_chongshu).onClick = (ButtonClick);
    }
    void ButtonClick(GameObject button)
    {
        if (button.name != "Num_delete" && button.name != "Num_Confirm" && button.name != "CloseBtn"&& button.name != "Num_chongshu")
        {
            NumEvent(int.Parse(button.name));
        }
        else if (button.name == "Num_delete")
        {
            if (showCount > 0)
            {
                TempNum.RemoveAt(TempNum.Count - 1);
                showCount--;
                SpriteNum[showCount].spriteName = " ";
                SpriteNum[showCount].gameObject.SetActive(false);
            }
        }
        else if (button.name == "Num_Confirm")
        {
            if (TempNum.Count == 6)
            {
                string str = "";
                for (int i = 0; i < 6; i++)
                {
                    str = str + TempNum[i].ToString();

                }
                StartCoroutine(SendCheckGame(int.Parse(str)));
            }
        }
        else if (button.name == "CloseBtn")
        {
            Debug.Log("..............");
            gameObject.SetActive(false);
        }
        else if (button.name == "Num_chongshu")
        {
            showCount = 0;
            TempNum.Clear();
            for (int i = 0; i < SpriteNum.Count; i++)
            {
                if (SpriteNum[i].gameObject.activeSelf == true)
                {
                    SpriteNum[i].gameObject.SetActive(false);
                }
            }

        }
    }
    void NumEvent(int num)
    {
        if (TempNum.Count < 6)
        {
            TempNum.Add(num);
            SpriteNum[showCount].spriteName = num.ToString();
            SpriteNum[showCount].GetComponent<UISprite>().MakePixelPerfect();
            SpriteNum[showCount].gameObject.SetActive(true);
            showCount++;
        }
        if (showCount == 6)
        {
            string str = "";
            for (int i = 0; i < 6; i++)
            {
                str = str + TempNum[i].ToString();

            }
            StartCoroutine(SendCheckGame(int.Parse(str)));
        }
    }

    /// <summary>
    /// 检查加入的房间是哪个游戏的
    /// </summary>
    /// <param name="roomId"></param>
    IEnumerator SendCheckGame(int roomId)
    {
        RoomCheckRequest rc = new RoomCheckRequest { RoomId = roomId };
        SocketManager socketManager = SocketManager.Instance;
        socketManager.SendProtobuf1(rc, MsgId.RoomCheckRequest);
        yield return new WaitForSeconds(0.1f);
        SendInGame(roomId);
    }

    void SendInGame(int roomid)
    {

        Debug.Log("gameType" + user.gameType);

        if (user.gameType == 1)
        {
            //麻将
            MahjongEnterRoomRequest RR = new Pb.MahjongEnterRoomRequest { RoomId = roomid };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(RR, MsgId.MahjongEnterRoomRequest);
        }
        else if (user.gameType == 2)
        {
            //牛牛
            NnEnterRoomRequest RR = new Pb.NnEnterRoomRequest { RoomId = roomid };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(RR, MsgId.NnEnterRoomRequest);
        }
        else if (user.gameType == 3)
        {
            //斗地主
            DdzEnterRoomRequest RR = new Pb.DdzEnterRoomRequest { RoomId = roomid };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(RR, MsgId.DdzEnterRoomRequest);
        }
        else if (user.gameType == 4)
        {
            //跑得快
            RunEnterRoomRequest RR = new Pb.RunEnterRoomRequest { RoomId = roomid };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(RR, MsgId.RunEnterRoomRequest);
        }
        else if (user.gameType == 101)
        {
            Debug.Log("牌九加入房间");
            PaiEnterRoundRequest PR = new PaiEnterRoundRequest { RoundType = 3, RoundNumber = roomid };
            SocketManager socketManager = SocketManager.Instance;
            socketManager.SendProtobuf1(PR, MsgId.PaiEnterRoundRequest);
        }

    }
}

